package cn.leestudio.springboot.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"cn.leestudio.springboot.full.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryFull")
public class DataSourceFullConfig {

    @Autowired
    @Qualifier("full")
    private DataSource full;


    @Bean
    public SqlSessionFactory sqlSessionFactoryFull() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(full); // 使用全局数据源, 连接所有允许的数据库
        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateFull() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryFull()); // 使用上面配置的Factory
        return template;
    }
}